package ldh.im.web.service;

import ldh.common.PageResult;
import ldh.im.business.dao.TenantDao;
import ldh.im.business.pojo.Tenant;
import ldh.im.business.pojo.where.TenantWhere;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: ldh
 * @date: 2021-06-06 10:17:43
*/
@Service("tenantService")
public class TenantService {

	@Resource
	private TenantDao tenantDao;
	
	@Transactional
	public Tenant insert(Tenant tenant) {
		int n = tenantDao.insert(tenant);
		if (n != 1) {
			throw new RuntimeException("Tenant insert error!");
		}
		return tenant;
	}
	
	@Transactional
	public Tenant insertSelective(Tenant tenant) {
		int n = tenantDao.insert(tenant);
		if (n != 1) {
			throw new RuntimeException("Tenant insertSelective error!");
		}
		return tenant;
	}

	@Transactional
	public Tenant updateById(Tenant tenant) {
		int  n = tenantDao.updateById(tenant);
		if (n != 1) {
			throw new RuntimeException("Tenant update error!");
		}
        return tenant;
     }

	@Transactional
	public Tenant updateNotNullById(Tenant tenant) {
		int  n = tenantDao.updateNotNullById(tenant);
		if (n != 1) {
			throw new RuntimeException("Tenant update error!");
		}
        return tenant;
     }


    @Transactional
    public void deleteById(Long id) {
		int n = tenantDao.deleteById(id);
		if (n != 1) {
			throw new RuntimeException("Tenant delete error!");
		}
    }

	@Transactional(readOnly = true)
	public Tenant getById(Long id) {
		Tenant tenant = tenantDao.getById(id);
		return tenant;
    }

	@Transactional(readOnly = true)
	public Tenant getByCode(String code) {
		Tenant tenant = tenantDao.getByCode(code);
		return tenant;
	}

	@Transactional(readOnly = true)
	public PageResult<Tenant> findByTenantWhere(TenantWhere tenantWhere) {
		long total = tenantDao.findTotalByTenantWhere(tenantWhere);
		List<Tenant> imageList = new ArrayList<Tenant>();
		if (total > 0) {
			imageList  = tenantDao.findByTenantWhere(tenantWhere);
		}
		
		return new PageResult<Tenant>(tenantWhere, total, imageList);
	}

	@Transactional(readOnly = true)
	public List<Tenant> getAll(TenantWhere tenantWhere) {
		return tenantDao.findByTenantWhere(tenantWhere);
	}

}
